package com.zdwhong.mysqlms.multidatasource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * @author ZDW
 * @since 2021-05-25 18:55
 * MyRoutingDataSource.java 方法determineCurrentLookupKey决定最终使用哪个数据源进行操作，若为空则使用默认数据源。
 */
@Slf4j
public class MyRoutingDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        log.info("线程名："+Thread.currentThread().getName()+"："+DBContextHolder.get());
        return DBContextHolder.get();
        /* if (DBContextHolder.get() != null) {
            System.out.println("线程名："+Thread.currentThread().getName()+":"+DBContextHolder.get());
            return DBContextHolder.get();
        } else {
            System.out.println("未匹配到指定数据库,默认切换到Master");
            return DBTypeEnum.MASTER;
        }*/
        //return DBContextHolder.get();
    }
}
